package com.admin.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.system.SystemUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import com.data.entity.Server;
import com.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统监控控制器
 *
 * @author: wangghua
 * @version: 1.0
 * @date: 2020-11-09 16:23
 **/
@RestController
@RequestMapping("/server")
@Api(tags = "系统监控")
/**
 * 系统监控
 */
public class ServerController {
    private Logger logger = LogUtil.getBussinessLogger();


    /**
     * 获取系统信息
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("系统信息")
    public Result getMyInfo() {
        Result result = new Result();
        try {
            Server server = new Server();
            //取得Java Virtual Machine Specification的信息
            server.setJvmSpecInfo(SystemUtil.getJvmSpecInfo());
            //取得Java Virtual Machine Implementation的信息
            server.setJvmInfo(SystemUtil.getJvmInfo());
            //取得Java Implementation的信息
            server.setJavaInfo(SystemUtil.getJavaInfo());
            //取得当前运行的JRE的信息
            server.setJavaRuntimeInfo(SystemUtil.getJavaRuntimeInfo());
            //取得OS的信息
            server.setOsInfo(SystemUtil.getOsInfo());
            //取得User的信息
            server.setUserInfo(SystemUtil.getUserInfo());
            //取得Runtime的信息
            server.setRuntimeInfo(SystemUtil.getRuntimeInfo());
            //取得Host的信息
            server.setHostInfo(SystemUtil.getHostInfo());
            //获得JVM最大内存
            server.setMaxMemory(FileUtil.readableFileSize(SystemUtil.getRuntimeInfo().getMaxMemory()));
            //获得JVM已分配内存
            server.setTotalMemory(FileUtil.readableFileSize(SystemUtil.getRuntimeInfo().getTotalMemory()));
            //获得JVM已分配内存中的剩余空间
            server.setFreeMemory(FileUtil.readableFileSize(SystemUtil.getRuntimeInfo().getFreeMemory()));
            //JVM最大可用内存
            server.setUsableMemory(FileUtil.readableFileSize(SystemUtil.getRuntimeInfo().getUsableMemory()));
            //JDK启动时间
            server.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getServerStartDate()));
            //JDK运行时间
            server.setRunTime(DateUtils.getDatePoor(DateUtils.getNowDate(), DateUtils.getServerStartDate()));
            result.setCode(Result.Code.SUCCESS);
            result.setData(server);
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }
}
